package com.wz.project.util;

import com.github.pagehelper.util.StringUtil;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具
 *
 * @author K
 */
public class DateUtill extends org.apache.commons.lang3.time.DateUtils{

    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_PATTERN = "yyyyMMdd";
    public final static String DATE_TIME = "yyyy-MM-dd";

    private static Calendar gregorianCalendar=new GregorianCalendar();

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }
    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }
    /**
     * 日期格式化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        }
        return null;
    }

    /**
     * 日期
     * yyyy/mm/dd 格式化
     *
     * @param pattern 格式
     * @return
     */
    public static String localDate(String pattern) {
        return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 时间
     * hh/mm/ss 格式化
     * @param pattern
     * @return
     */
    public static String localTime(String pattern){
        return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期时间
     * yyyy/mm/dd hh/mm/ss
     * @param pattern
     * @return
     */
    public static String localDateTime(String pattern){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern));
    }


    /**
     * 字符串转日期
     *
     * @param strDate
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date stringToDate(String strDate, String pattern) throws ParseException {
        if (StringUtil.isNotEmpty(strDate)) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(strDate);
        }
        return null;

    }

    /**
     * 时间计算
     *
     * @param date1
     * @param date2
     * @return
     */
    public static String daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return String.valueOf(between_days);
    }


    public static String getnowtime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        return sdf.format(new Date());
    }
    //判断批次中日期是否有效
    public static boolean checkDate(String str,String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
        try{
            sdf.setLenient(false);
            sdf.parse(str);
        }
        catch (Exception e){
            return false;
        }
        return true;
    }
    /**
    * 获取当前月的第一天
    *
    * @return date
    */
    public static Date getFirstDayOfMonth() {
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
        return gregorianCalendar.getTime();
     }
    /**
    * 获取当前月的最后一天
    *
    * @return
    */
    public static Date getLastDayOfMonth() {
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
        gregorianCalendar.add(Calendar.MONTH, 1);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return gregorianCalendar.getTime();
    }
    /**
    * 获取指定月的第一天
    *
    * @param date
    * @return
    */
    public static Date getFirstDayOfMonth(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
        return gregorianCalendar.getTime();
    }
    /**
    * 获取指定月的最后一天
    *
    * @param date
    * @return
    */
    public static Date getLastDayOfMonth(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
        gregorianCalendar.add(Calendar.MONTH, 1);
        gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
        return gregorianCalendar.getTime();
    }
    /**
    * 获取日期前一天
    *
    * @param date
    * @return
    */
    public static Date getDayBefore(Date date) {
        gregorianCalendar.setTime(date);
        int day = gregorianCalendar.get(Calendar.DATE);
        gregorianCalendar.set(Calendar.DATE, day - 1);
        return gregorianCalendar.getTime();
    }
    /**
     * 日期的后一天
     * @param date
     * @return
     */
    public static Date getDayAfter(Date date) {
        gregorianCalendar.setTime(date);
        int day = gregorianCalendar.get(Calendar.DATE);
        gregorianCalendar.set(Calendar.DATE, day + 1);
        return gregorianCalendar.getTime();
    }

    public static void main(String[] args) throws ParseException {

       /* System.err.println("从系统时钟获取当前日期:" + LocalDate.now());
        System.err.println("格式化当前日期:" + LocalDate.now().format(DateTimeFormatter.ofPattern(DATE_PATTERN)));
        System.err.println("当前日期在这周的第几天:"+LocalDateTime.now().getDayOfWeek());

        System.err.println("--------------------");

        Date date1 = new Date();
        String strDate = "2020-11-23";
        Date date2 = stringToDate(strDate,DATE_TIME);
        System.out.println(date1);
        System.out.println(daysBetween(date2,date1));
        //传入正常值返回true
        Optional<String> s = Optional.ofNullable("1");
        System.err.println(s.isPresent());*/
        System.out.println("-------");
        System.out.println(format(new Date(), DATE_TIME_PATTERN));
        System.out.println("-------");
    }
}
